﻿using System.Diagnostics;
using System.IO;
using ETModel;
using UnityEditor;
using UnityEngine;

namespace ETEditor
{

	public class Proto2CSEditor: EditorWindow
	{
        //框架级的不需要开放给开发者
        //[MenuItem("Tools/Proto2CS")]
        //public static void AllProto2CS()
        //{
        //	Process process = ProcessHelper.Run("dotnet", "Proto2CS.dll", "../Proto/", true);
        //	Log.Info(process.StandardOutput.ReadToEnd());
        //	AssetDatabase.Refresh();
        //}

        const string path = @"./Assets/ET/Res/Config/DeveloperConfig.json";

        private DeveloperConfig devpConfig;
        public void Awake() {
            if (File.Exists(path)) {
                this.devpConfig = JsonHelper.FromJson<DeveloperConfig>(File.ReadAllText(path));
            } else {
                this.devpConfig = new DeveloperConfig();
            }
        }

        [MenuItem("Tools/Proto2CS")]
        public static void AllProto2CS() {
            GetWindow<Proto2CSEditor>();
        }

        public void OnGUI() {
            this.devpConfig.ProtoDir = EditorGUILayout.TextField("Proto目录(相对Unity目录):", this.devpConfig.ProtoDir);
            this.devpConfig.InnerMessageProtoName = EditorGUILayout.TextField("inner消息proto文件名:", this.devpConfig.InnerMessageProtoName);
            this.devpConfig.OuterMessageProtoName = EditorGUILayout.TextField("outer消息proto文件名:", this.devpConfig.OuterMessageProtoName);
            this.devpConfig.HotfixMessageProtoName = EditorGUILayout.TextField("hotfix消息proto文件名:", this.devpConfig.HotfixMessageProtoName);
            this.devpConfig.InnerMessageOutput = EditorGUILayout.TextField("inner类输出目录(相对Proto目录):", this.devpConfig.InnerMessageOutput);
            this.devpConfig.OuterMessageOutput = EditorGUILayout.TextField("outer类输出目录(相对Proto目录):", this.devpConfig.OuterMessageOutput);
            this.devpConfig.HotfixMessageOutput = EditorGUILayout.TextField("hotfix类输出目录(相对Proto目录):", this.devpConfig.HotfixMessageOutput);
            this.devpConfig.StartOpCode = EditorGUILayout.IntField("OpCode开始值(2w~6w):", this.devpConfig.StartOpCode);
            this.devpConfig.OpCodePrefix = EditorGUILayout.TextField("OpCode类名前缀:", this.devpConfig.OpCodePrefix);

            if (GUILayout.Button("生成")) {

                if (string.IsNullOrEmpty(this.devpConfig.ProtoDir)) {
                    Log.Error("请输入Proto目录(相对Unity目录),如\"../DemoProto/\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.InnerMessageProtoName)) {
                    Log.Error("请输入Inner消息proto文件名,如\"DemoInnerMessage.proto\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.OuterMessageProtoName)) {
                    Log.Error("请输入Outer消息proto文件名,如\"DemoOuterMessage.proto\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.HotfixMessageProtoName)) {
                    Log.Error("请输入Hotfix消息proto文件名,如\"DemoHotfixMessage.proto\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.InnerMessageOutput)) {
                    Log.Error("请输入Inner消息类输出目录(相对Proto目录),如\"../Server/Demo.Model/Module/Message/\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.OuterMessageOutput)) {
                    Log.Error("请输入Outer消息类输出目录(相对Proto目录),如\"../Unity/Assets/ET/Model/Demo/Module/Message/\"");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.HotfixMessageOutput)) {
                    Log.Error("请输入Hotfix消息类输出目录(相对Proto目录),如\"../Unity/Assets/ET/Hotfix/Demo/Module/Message/\"");
                    return;
                }
                if (this.devpConfig.StartOpCode<20000 || this.devpConfig.StartOpCode>600000) {
                    Log.Error("消息OpCode开始值需要在20000~60000之间(包含)");
                    return;
                }
                if (string.IsNullOrEmpty(this.devpConfig.OpCodePrefix)) {
                    Log.Error("请输入OpCode类名前缀,如\"Demo\"");
                    return;
                }

                Process process = ProcessHelper.Run("dotnet", $"Proto2CS.dll" +
                    $" /InnerMessageOutput:\"{this.devpConfig.InnerMessageOutput}\"" +
                    $" /OuterMessageOutput:\"{this.devpConfig.OuterMessageOutput}\"" +
                    $" /HotfixMessageOutput:\"{this.devpConfig.HotfixMessageOutput}\"" +
                    $" /StartOpCode:{this.devpConfig.StartOpCode} /OpCodePrefix:{this.devpConfig.OpCodePrefix}" +
                    $" /InnerMessageProtoName:{this.devpConfig.InnerMessageProtoName}" +
                    $" /OuterMessageProtoName:{this.devpConfig.OuterMessageProtoName}" +
                    $" /HotfixMessageProtoName:{this.devpConfig.HotfixMessageProtoName}", this.devpConfig.ProtoDir, true);
                Log.Info(process.StandardOutput.ReadToEnd());
                AssetDatabase.Refresh();

                File.WriteAllText(path, JsonHelper.ToJson(this.devpConfig));
            }
        }
    }

    public class DeveloperConfig {
        public string ProtoDir;
        public string InnerMessageProtoName;
        public string OuterMessageProtoName;
        public string HotfixMessageProtoName;
        public string InnerMessageOutput;
        public string OuterMessageOutput;
        public string HotfixMessageOutput;
        public int StartOpCode;
        public string OpCodePrefix;
    }
}
